package com.i8office.android.zhihuixinwu.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.cyan.tools.annotation.ActivityFragmentInject;
import com.cyan.tools.base.BaseFragment;
import com.i8office.android.zhihuixinwu.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import view.ThreePointLoadingView;


/**
 * Created by pc on 2018/7/7.
 */
@ActivityFragmentInject(
        contentViewId = R.layout.fragment_pdf
)
public class PDFShowFragment extends BaseFragment {
    @BindView(R.id.content)
    FrameLayout content;
    @BindView(R.id.loading)
    ThreePointLoadingView loading;
    WebView webView;
    private boolean isChangeScale = false;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    protected void initView(View rootView) {
        ButterKnife.bind(this, rootView);
        initWebView();
        String pdfUrl = getArguments().getString("pdfUrl");
//        preView("http://p5grppofr.bkt.clouddn.com/pdf-js-demo.pdf");
        preView(pdfUrl);
    }

//    @Override
//    public void onHiddenChanged(boolean hidden) {
//        super.onHiddenChanged(hidden);
//        if(!hidden){
//            webView.reload();
//        }
//    }

    public void initWebView() {
        if (content.getChildCount() > 0)
            content.removeAllViews();
        webView = new WebView(getContext());
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(false);

        settings.setUseWideViewPort(true);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                loading.play();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                loading.stop();
            }

            @Override
            public void onScaleChanged(WebView view, float oldScale, float newScale) {
                super.onScaleChanged(view, oldScale, newScale);
                isChangeScale = true;
            }
        });
        content.addView(webView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isChangeScale) {
            isChangeScale = false;
            initWebView();
            preView("http://p5grppofr.bkt.clouddn.com/pdf-js-demo.pdf");
        }
    }

    /**
     * 预览pdf
     *
     * @param pdfUrl url或者本地文件路径
     */
    private void preView(String pdfUrl) {
        //1.只使用pdf.js渲染功能，自定义预览UI界面
//        webView.loadUrl("file:///android_asset/pdfjs/web/viewer.html?" + pdfUrl);
        webView.loadUrl("file:///android_asset/index.html?" + pdfUrl);
        //2.使用mozilla官方demo加载在线pdf
//        mWebView.loadUrl("http://mozilla.github.io/pdf.js/web/viewer.html?file=" + pdfUrl);
        //3.pdf.js放到本地
//        mWebView.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + pdfUrl);
        //4.使用谷歌文档服务
//        mWebView.loadUrl("http://docs.google.com/gviewembedded=true&url=" + pdfUrl);
    }
}
